package ge.telasi.tasks.ui.search;

import ge.telasi.tasks.search.Condition;
import ge.telasi.tasks.ui.UIUtils;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.swing.Icon;

/**
 * Task priority field.
 * 
 * @author dimitri
 */
public class TaskPriorityField extends Field {
    private List<Integer> priorities;

    public TaskPriorityField() {
        super("taskSearch.priority", "პრიორიტეტი");
    }

    public List<Integer> getPriorities() {
        return priorities;
    }

    public void setPriorities(List<Integer> priorities) {
        this.priorities = priorities;
    }
    
    @Override
    public String getType() {
        return "task_priority";
    }

    @Override
    public Icon getIcon() {
        return UIUtils.getIcon16x16("email_priority.png");
    }

    @Override
    public Condition getCondition() {
        // empty conditions
        if (priorities == null || priorities.isEmpty()) {
            return null;
        }

        // create where condition
        StringBuilder b = new StringBuilder();
        Map<String, Object> substitutions = new HashMap<String, Object>();
        b.append(" ( ");
        b.append(getSysName());
            b.append(" IN ( ");
        boolean first = true;
        for (Integer status : priorities) {
            if (!first) {
                b.append(", ");
            }
            String fieldname = getNextField();
            b.append(":");
            b.append(fieldname);
            substitutions.put(fieldname, status);
            first = false;
        }
        b.append(" ) ) ");
        String where = b.toString();

        // build condition
        Condition condition = new Condition();
        condition.setCondition(where);
        condition.getSubstitutions().putAll(substitutions);
        return condition;
    }

}